/**
 * 小时倒计时
 * 例示: new Countdown([1,2,3,4,5,6])		括号内的数组格式为整数时间，1~24，每天必须以24点结束
 */

class Countdown {
	constructor(fixedTimes) {
		this.fixedTimes = fixedTimes
	}
	/* 计算距离现在最近的时间 */
	comparison() {
		let hour = new Date().getHours()
		let arr = this.fixedTimes.filter(v => {
			return (v - hour) > 0
		})
		return Math.min(...arr)
	}
	/* 设置时间并求出时间差 */
	settingTime() {
		let date = new Date()
		date.setHours(this.comparison())
		date.setMinutes(0)
		date.setSeconds(0)
		if (date - new Date() >= 0) {
			return date - new Date()
		} else {
			this.settingTime()
		}
	}
	/* 进行格式化时间戳并返回一个对象 {'h':时,'m':分,'s':秒} */
	get() {
		let remainingTime = this.settingTime()
		let h = parseInt(remainingTime / 1000 / 60 / 60)
		let m = parseInt(remainingTime / 1000 / 60 % 60)
		let s = parseInt(remainingTime / 1000 % 60)
		return {
			h: h < 10 ? `0${h}` : h,
			m: m < 10 ? `0${m}` : m,
			s: s < 10 ? `0${s}` : s
		}
	}
}